import 'package:cashbook/model/user_vo.dart';
import 'package:cashbook/utils/dialogutil.dart';
import 'package:cashbook/utils/manager.dart';
import 'package:cashbook/utils/toast.dart';
import 'package:flutter/material.dart';
import 'package:common_utils/src/log_util.dart';

class ForgotPasswordPage extends StatefulWidget {
  String title;

  ForgotPasswordPage({Key key, this.title}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return ForgotPasswordState();
  }
}

class ForgotPasswordState extends State<ForgotPasswordPage> {
  var answer1 = TextEditingController();
  var answer2 = TextEditingController();
  var newPsd = TextEditingController();
  var snewPsd = TextEditingController();
  var nameC = TextEditingController();

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    var textEditOutlineInputBorder = OutlineInputBorder(
      borderRadius: BorderRadius.circular(5),
      borderSide: BorderSide(color: Theme.of(context).primaryColor),
    );
    var testStyle =
        TextStyle(color: Colors.blue, decoration: TextDecoration.underline);
    return Scaffold(
      appBar: AppBar(
        title: Text(this.widget.title),
      ),
      body: Container(
        padding: const EdgeInsets.only(left: 5, right: 5),
        child: Column(
          children: <Widget>[
            Card(
              child: Container(
                margin: const EdgeInsets.only(left: 10, right: 10, bottom: 10),
                child: Column(
                  children: <Widget>[
                    TextField(
                      controller: nameC,
                      decoration: setInputDecoration('账户名称'),
                    ),
                    TextField(
                      controller: newPsd,
                      decoration: setInputDecoration('新密码'),
                    ),
                    TextField(
                      controller: snewPsd,
                      decoration: setInputDecoration('确认新密码'),
                    ),
                  ],
                ),
              ),
            ),
            Card(
              child: Container(
                margin: const EdgeInsets.only(left: 10, right: 10, bottom: 10),
                child: Column(
                  children: <Widget>[
                    Container(
                      height: 40,
                      alignment: Alignment.centerLeft,
                      child: Text(Manager.instance.ask1),
                    ),
                    TextField(
                      controller: answer1,
                      decoration: setInputDecoration('答案'),
                    ),
                    Container(
                      height: 40,
                      alignment: Alignment.centerLeft,
                      child: Text(Manager.instance.ask1),
                    ),
                    TextField(
                      controller: answer2,
                      decoration: setInputDecoration('答案'),
                    ),
                  ],
                ),
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: 20),
              child: RaisedButton(
                color: Colors.blue,
                onPressed: forgotAction,
                child: Text('找回'),
              ),
            )
          ],
        ),
      ),
    );
  }

  setInputDecoration(String hintText) {
    return InputDecoration(hintText: hintText);
  }

  void forgotAction() async {
    if (null == nameC || nameC.text.toString().length <= 0) {
      Toast.toast(context, '账户名不能为空');
      return;
    }
    if (null == newPsd || newPsd.text.toString().length <= 0) {
      Toast.toast(context, '新密码不能为空');
      return;
    }
    if (null == snewPsd || snewPsd.text.toString().length <= 0) {
      Toast.toast(context, '确认新密码不能为空');
      return;
    }
    if (snewPsd.text.toString() != newPsd.text.toString()) {
      Toast.toast(context, '2次密码不一致');
      return;
    }
    //print('找回');

    User newUser = User();

    newUser.name = nameC.text;
    newUser.psd = newPsd.text;
    newUser.answer1 = answer1.text;
    newUser.answer2 = answer2.text;
    UserProvider();
    User dbUser = await UserProvider().findByName(newUser);
    if (dbUser == null) {
      Toast.toast(context, '没有找到「${newUser.name}」的账户！');
      return;
    }
    if (dbUser.answer2 != newUser.answer2 ||
        dbUser.answer1 != newUser.answer1) {
      Toast.toast(context, '你回答的问题不正确！');
      return;
    }
    dbUser.psd = newUser.psd;
    int i = await UserProvider().update(dbUser);
    //print('=====>更新结果${i}');
    if (1 == i){
      DialogUtil.showAlertDialog(context, '提示', '修改密码成功！',sure:(){
        Navigator.of(context).pop();
        //Toast.toast(context, '确定按钮');
        LogUtil.e('修改密码成功');
      },);
    }else{
      LogUtil.e('修改密码失败');
    }
  }

}
